<template>
		<view class="login-container">
			<!-- 提示登录的图标 -->
			<uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
			<!-- 登录按钮 -->
			<button type="primary" class="btn-login" open-type="getUserInfo" @getuserinfo="getUserInfoHandler" >一键登录</button>
			<!-- 登录提示 -->
			<view class="tips-text">登录后尽享更多权益</view>
		</view>
</template>

<script>
	import { mapMutations,mapState} from 'vuex'
	export default {
		name:"my-login",
		data() {
			return {
				
			}
		},
		computed:{
			...mapState('m_user',['redirectInfo'])
		},
		methods:{
			...mapMutations('m_user',['updateUserInfo','updateToken','updateRedirectInfo']),
			getUserInfoHandler(e){
				// console.log(e)
				// console.log(e.detail.userInfo)
				this.updateUserInfo(e.detail.userInfo)
				this.getToken(e.detail)
			},
			async getToken(info){
				// 封装了 wx.login登录
				const [err,res] = await uni.login().catch(err=>err)
				if(err || res.errMsg !== 'login:ok'){
					return uni.showToast({
						title:"登录失败"
					})
				}
				// console.log(res)
				// console.log(res.code)
				// console.log(info)
				//登录调用微信接口 实现登录功能
				const query = {
					code:res.code,//用户登录凭证
					encryptedData: info.encryptedData,
					iv: info.iv,
					rawData: info.rawData,
					signature: info.signature
				}
				
				//调用微信接口
				const  loginResult = await this.$myRequest({
					url:'/getToken',
					method:'post',
					data:query
				})
				console.log(loginResult.data.token)
				this.updateToken(loginResult.data.token)
				// 调用回去的函数
				this.navigateBack()
			},
			navigateBack(){
				//获取到vuex中的对象
				if(this.redirectInfo && this.redirectInfo.openType === 'switchTab'){
					//成功在跳回去
					uni.switchTab({
						url:this.redirectInfo.from,
						complete: () => {
						    this.updateRedirectInfo(null)
						}
					})
				}
			}
			
			
			
		}
	}
</script>

<style lang="scss">
	.login-container {
	// 登录盒子的样式
		height: 750rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: white;
		position: relative;
		overflow: hidden;
		&::after{
			content: ' ';
			display: block;
			position: absolute;
			width: 100%;
			height: 40px;
			left: 0;
			bottom: 0;
			background-color: red;
			border-radius: 100%;
			transform: translateY(50%);
		}
		//登录按钮的样式
		.btn-login {
			width: 90%;
			border-radius: 100px;
			margin-top: 50px;
			margin-bottom: 10px;
			background-color: #c00000;
		}
		uni-icons{
			margin: 20px;
		}
		// 按钮下方提示消息的样式
		.tips-text {
			font-size: 12px;
			color: gray;
		}
	}

</style>